package com.sias;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author 吴文杰
 * @version 1.0
 * @since 2023/6/14 22:15
 */
public class JdkProxyDemo {
  public static void main(String[] args) {

    Target target = new Target();

    Object instance = Proxy.newProxyInstance(JdkProxyDemo.class.getClassLoader(), Target.class.getInterfaces(), new InvocationHandler() {
      @Override
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("增强前");
        Object invoke = method.invoke(target);
        System.out.println("增强后");
        return invoke;
      }
    });
    Foo instance1 = (Foo) instance;
    instance1.foo();
  }

  interface Foo {
    void foo();
  }

  static class Target implements Foo {

    @Override
    public void foo() {
      System.out.println("target foo");
    }
  }
}
